package com.sdyeda.readiot.fx;

import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.sdyeda.readiot.config.Setting;
import com.sdyeda.readiot.data.SetData;
import com.sdyeda.readiot.data.SetDataService;

import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.fxml.FXML;
import javafx.scene.control.Alert;
import javafx.scene.control.Button;
import javafx.scene.control.ChoiceBox;
import javafx.scene.control.RadioButton;
import javafx.scene.control.Spinner;
import javafx.scene.control.SpinnerValueFactory;
import javafx.scene.control.TextField;
import javafx.scene.control.Toggle;
import javafx.scene.control.ToggleGroup;
import javafx.scene.control.Alert.AlertType;
import javafx.stage.Stage;

@Component
public class SetAllController {

	private Stage stage;
	
	@Autowired
	private SetDataService setservice;
	
	@FXML
	private Button btnSave,btnClose;
	
	@FXML
	private RadioButton rbDelay,rbCron;
	private ToggleGroup tgType = new ToggleGroup();
	
	
	@FXML
	private TextField txtPassword,txtCron,txtDelay;
	@FXML
	private ChoiceBox<String> cbStep;
	
	@FXML
	private Spinner<Integer> spinRetry;
	
	private Map<String,SetData> sets;
	
	@FXML
	public void initialize() {

		//初始化定时任务类型Ridio
		rbDelay.setToggleGroup(tgType);
		rbCron.setToggleGroup(tgType);
		
		tgType.selectedToggleProperty().addListener(new ChangeListener<Toggle>() {

			@Override
			public void changed(ObservableValue<? extends Toggle> observable, Toggle oldValue, Toggle newValue) {
				if (newValue == rbDelay) {
					txtCron.setDisable(true);
					txtDelay.setDisable(false);
					cbStep.setDisable(false);
				} else {
					txtCron.setDisable(false);
					txtDelay.setDisable(true);
					cbStep.setDisable(true);
				}
			}
		});

		//初始化时间间隔下拉框
		cbStep.getItems().add("小时");
		cbStep.getItems().add("分钟");
		cbStep.getItems().add("秒");

		//按钮任务
		btnClose.setOnAction(o->{
			stage.close();
		});
		
		btnSave.setOnAction(o->{
			if(SaveData()) {
				new Alert(AlertType.INFORMATION, "保存成功！").show();
				stage.close();
			}
		});

		LoadData();
		
	}
	
	void LoadData() {
		sets = setservice.getAllSetData();
		
		//定时类型
		if("delay".equals(sets.get(Setting.SCHEDULED_TYPE).getValue())) {
			rbDelay.setSelected(true);
		}
		if("cron".equals(sets.get(Setting.SCHEDULED_TYPE).getValue())) {
			rbCron.setSelected(true);
		}

		
		txtPassword.setText(sets.get(Setting.SET_PASSWORD).getValue());
		txtCron.setText(sets.get(Setting.SCHEDULED_CRON).getValue());
		
		//延时转换
		txtDelay.setText(sets.get(Setting.SCHEDULED_DELAY).getValue());				
		Long delay = Long.valueOf(sets.get(Setting.SCHEDULED_DELAY).getValue());
		if (delay/3600000.0 == (int)(delay/3600000.0)) {
			cbStep.setValue("小时");
			txtDelay.setText(String.valueOf(delay/3600000L));
		}
		else if(delay/60000.0==(int)(delay/60000.0)) {
			cbStep.setValue("分钟");
			txtDelay.setText(String.valueOf(delay/60000L));
		}
		else {
			cbStep.setValue("秒");
			txtDelay.setText(String.valueOf(delay/1000L));
		}
		
		//重试次数
		int value = Integer.valueOf(sets.get(Setting.RETRYTIMES).getValue());
		spinRetry.setValueFactory(new SpinnerValueFactory.IntegerSpinnerValueFactory(1, 10,value,1));

	}
	
	boolean SaveData() {
		
		Long delay = 0L;
		Long rate = 60L;
		
		try {
			delay = Long.valueOf(txtDelay.getText());
			if(delay<=0) {
				new Alert(AlertType.ERROR,"时间间隔输入错误，请输入正整数。").show();
				return false;
			}
		}
		catch(Exception e){
			new Alert(AlertType.ERROR,"时间间隔输入错误，请输入正整数。").show();
			return false;
		}
		
		if(cbStep.getValue().equals("小时")) 
			rate = 3600L;
		else if(cbStep.getValue().equals("分钟"))
			rate = 60L;
		else
			rate = 1L;
		
		SetData value;
		
		//存储延时
		value = sets.get(Setting.SCHEDULED_DELAY);
		value.setValue(String.valueOf(delay*rate*1000L));
		setservice.save(value);
		
		//存储重试次数
		value = sets.get(Setting.RETRYTIMES);		
		value.setValue(String.valueOf(spinRetry.getValue()));
		setservice.save(value);
		
		//密码
		value = sets.get(Setting.SET_PASSWORD);
		value.setValue(txtPassword.getText());
		setservice.save(value);
		
		//定时类型
		value = sets.get(Setting.SCHEDULED_TYPE);
		value.setValue(rbDelay.isSelected()?"delay":"cron");
		setservice.save(value);
		
		//定时配置
		value = sets.get(Setting.SCHEDULED_CRON);
		value.setValue(txtCron.getText());
		setservice.save(value);
			
		return true;
	}

	public Stage getStage() {
		return stage;
	}

	public void setStage(Stage stage) {
		this.stage = stage;
	}
}
